package com.bsg.upm.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.ComboService;

/**
 * 套餐管理控制层
 * 
 * @author HCK
 *
 */
@Controller
@RequestMapping("/v1.0/combos")
public class ComboController {

	@Resource
	private ComboService comboService;

	/**
	 * 【查询】
	 * 
	 * @param definitionServCode
	 *            服务定义代码
	 * @return 操作结果
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listCombo(@RequestParam(value = "serv_type", required = true) String definitionServCode,
			HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = comboService.list(definitionServCode);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【查询单条记录】
	 * 
	 * @param comboId
	 *            套餐编码
	 * @return 操作结果
	 */
	@RequestMapping(value = "/{combo}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getCombo(@PathVariable("combo") String comboId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = comboService.get(comboId);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
